/*
 * url:链接
 * method:请求方式post,get;
 * params:post方式的参数字符串
 * targetelement：要显示的html标签的ID；
 */
function toDoXHR(url,method,params,targetelement){
    //新建xhr对象
	myreq = new RequestClass();
	//将参数传入
	myreq.setUrl(url);
	myreq.setReq(netreq);
	myreq.setMethod(method);
	myreq.setParams(params);
	myreq.setTargetelement(targetelement);
	//方法执行
	myreq.run();
	
}
netreq = new XMLHttpRequest();

var RequestClass = function(){
	
	this.req =netreq; 
	this.url = "myurl";
	this.method = "POST";
	this.params = null;
	this.targetelement ="mytips";
	
	this.getUrl = function(){
		return this.url;
	};
	this.setReq=function(req){
		this.req=req;
	};
	this.setUrl=function(url){
		this.url=url;
	};
	this.setMethod=function(method){
		this.method=method;
	};
	this.setParams=function(params){
		this.params=params;
	};
	this.setTargetelement=function(params){
		this.targetelement=params;
	};
	
	this.run = function(){
		var MyClass =this;
		var targetelement =this.targetelement;
		
		this.req.onreadystatechange=function(){
			if(netreq.readyState==4){
				if(netreq.status==200){
					document.getElementById(targetelement).innerHTML=netreq.responseText;
				}else{
					alert("请求有误");
				}
			}
		};
		
		this.req.open(this.method, this.url);
		
		if(this.method.toUpperCase()=="POST"){
			this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.req.send(this.params);
		}else{
			this.req.send(null);
		}
		
	};
	
};

